// ConsoleDlg.cpp : implementation file
//
#include "stdafx.h"
#include "afxwin.h"
//#include "Def.h"
#include "afxcmn.h"
#include <string>
#include "ConsoleDlg.h"

// CConsoleDlg

IMPLEMENT_DYNAMIC(CConsoleDlg, CWnd)

CConsoleDlg::CConsoleDlg():
m_OutputConsole(),
m_CmdConsole(this)
{
	m_Command.Empty();
}

CConsoleDlg::~CConsoleDlg()
{
}


BEGIN_MESSAGE_MAP(CConsoleDlg, CWnd)
	ON_WM_SIZE()
	ON_WM_DESTROY()
	ON_WM_CLOSE()
END_MESSAGE_MAP()

BOOL CConsoleDlg::Create( CWnd* pParent /*= NULL*/ )
{
	CString strMyClass = AfxRegisterWndClass(
		CS_VREDRAW | CS_HREDRAW,0,
		(HBRUSH) ::GetStockObject(WHITE_BRUSH)
		);
	
	BOOL r = CWnd::CreateEx(0,strMyClass.GetBuffer(),_T("Console Window"),WS_CAPTION|WS_POPUPWINDOW| WS_THICKFRAME,CRect(50,50,550,650),pParent,0);
	if ( r == FALSE)
		return r;
	CRect rect;
	GetClientRect(rect);
	AfxInitRichEdit();
	CPoint cmdbottomright(rect.BottomRight().x,(rect.BottomRight().y)*2/3);
	CPoint outputtopleft(rect.TopLeft().x,cmdbottomright.y+2);
	CRect CmdRect(rect.TopLeft(),cmdbottomright);
	CRect OutputRect(outputtopleft,rect.BottomRight());
	r = m_CmdConsole.Create(WS_CHILD|WS_VISIBLE|ES_MULTILINE |WS_VSCROLL| ES_AUTOVSCROLL | WS_BORDER | WS_TABSTOP,CmdRect,this,1234);
	m_CmdConsole.ClearAll();
	r=m_OutputConsole.Create(WS_CHILD|WS_VISIBLE|ES_MULTILINE |WS_VSCROLL| ES_AUTOVSCROLL | WS_BORDER | WS_TABSTOP,OutputRect,this,1235);
	m_OutputConsole.Clear();
	m_OutputConsole.SetReadOnly(TRUE);
	OnInit();
	return r;
}


// CConsoleDlg message handlers



void CConsoleDlg::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);
	CRect rect;
	GetClientRect(rect);
	if(m_CmdConsole.GetSafeHwnd() != NULL)
		m_CmdConsole.SetWindowPos(&CWnd::wndTop,0,0,rect.Width(),rect.Height(),SWP_SHOWWINDOW);
	// TODO: Add your message handler code here
}

void CConsoleDlg::Destory()
{

}
void CConsoleDlg::OnDestroy()
{
	CWnd::OnDestroy();

	//delete this;
	// TODO: Add your message handler code here
}

void CConsoleDlg::OnClose()
{
	// TODO: Add your message handler code here and/or call default

	CWnd::OnClose();
	//delete this;
}

void CConsoleDlg::OnInit()
{
	m_CmdConsole.Init(
		CString(_T("Console is Started!")));
}

void CConsoleDlg::Output( CString str,COLORREF color/*=(0,0,0)*/ )
{
	int end = m_OutputConsole.GetTextLengthEx(GTL_NUMCHARS);
	m_OutputConsole.SetSel(-1,-1);
	m_OutputConsole.ReplaceSel(str.GetBuffer());

	CHARFORMAT cf;
	cf.cbSize = sizeof(cf);
	m_OutputConsole.GetDefaultCharFormat(cf);
	DWORD dwEffects = 0;
	cf.dwEffects = dwEffects;
	cf.crTextColor = color;
	m_OutputConsole.SetSel(end,m_OutputConsole.GetTextLengthEx(GTL_NUMCHARS));
	m_OutputConsole.SetSelectionCharFormat(cf);
	m_OutputConsole.HideSelection(TRUE,TRUE);
	m_OutputConsole.SetSel(-1,-1);
	m_OutputConsole.ReplaceSel(_T("\n"));
	m_OutputConsole.HideSelection(TRUE,TRUE);
	m_OutputConsole.PostMessage(WM_VSCROLL,SB_BOTTOM);
}

void CConsoleDlg::OnConsoleCommand( CString& strCmd )
{
	m_Command.Empty();
	m_Command=strCmd;
}

CString CConsoleDlg::ReadCommand()
{
	CString tmpCommand=m_Command;
	m_Command.Empty();
	return tmpCommand;
}
